/**
 * build_project
 * <p>
 * Copyright 2014 cayden.cn, Inc. All rights reserved.
 *
 * @author Administrator
 * @date 2021-06-18 14:54
 */
package com.cayden.config.boot;

import com.cayden.base.exception.CaydenBaseException;
import com.cayden.base.utils.SystemUtils;
import com.cayden.base.utils.constants.OSConstants;
import com.cayden.config.EnvironmentHolder;
import com.cayden.config.boot.listener.CaydenApplicationRunListener;
import com.cayden.config.init.DefaultLogPath;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;

import java.io.File;
import java.io.IOException;

/**
 * @author cayden
 * @date 2021-06-18 14:54
 */
public class Apps {

    public static String logBasePath = null;

    private static String logPath = null;
    private static String dataPath = null;

    private static String appName = null;

    private static String osName = null;

    private static int runningPort = 0;

    private static String DEFUALT_BASE_PACKAGE = "";

    private static final String MAC_OS_EVN_BASE_PACKAGE = "/Users/cayden/project/log/webapps/";

    private static final String WIN_AND_LINUX_BASE_PACKAGE = "/var/log/webapps/";

    private static DefaultLogPath DEFAULT_LOG_PATH;

    public static final String BASE_PACKAGE = "cayden.basePackage";

    private static final String OS_VERSION = "cayden.os";
    /**
     * 环境
     */
    public static final String DEV_MODE_KEY = "cayden.devMode";

    // 默认为windows
    public static String SYS_OS = "windows";

    public static String SPRING_PROFILE_ACTIVE = "spring.profiles.active";

    private static Boolean isTest = null;

    public static String getLogPath() {
        initSysOSInfo();
        if (logPath != null){
            return logPath;
        }

        logPath = DEFUALT_BASE_PACKAGE + getAppName() + "/";

        try {
            FileUtils.forceMkdir(new File(logPath));
        } catch (IOException var1) {
            throw new CaydenBaseException("创建日志目录失败", var1);
        }

        return logPath;

    }

    public static String getAppName() {
        if (StringUtils.isNotBlank(appName)) {
            return appName;
        }
        appName = System.getProperty("cayden.appName");
        return appName;
    }

    public static int getRunningPort(){
        if (runningPort != 0) {
            return runningPort;
        }
        runningPort = Integer.valueOf(System.getProperty("server.port"));
        return runningPort;
    }

    public static String getBasePackage() {
        return System.getProperty(BASE_PACKAGE);
    }

    public static ApplicationContext getApplicationContext(){
        return ApplicationContextHolder.getApplicationContext();
    }

    public static Environment getEnvironment(){
        return EnvironmentContextHolder.getEVN();
    }

    private static void initSysOSInfo() {
        SYS_OS = SystemUtils.getOsName();
        System.setProperty(OS_VERSION,SYS_OS);
    }

    public static <T> T buildProperties(Class<T> clazz ) {
        return EnvironmentHolder.buildProperties(clazz);
    }

    /**
     * 是否是开发模式
     */
    public static boolean isDevMode() {
        return Boolean.valueOf(System.getProperty(DEV_MODE_KEY));
    }

    /**
     * 提供优雅关闭app的能力,可在任何地方调用
     * 这个方法会停止app服务,请谨慎使用
     */
    public static void shutdownApp(){
        CaydenApplicationRunListener.shutdownApp();
        // 把spring exit 放到了shutdownApp 中
        //SpringApplication.exit(getApplicationContext(),() -> 0);
    }

    public static void initDefaultBasePackage(String basePackage){
        DEFUALT_BASE_PACKAGE = basePackage;
    }

    public static void getDbEnvironment(){
        // todo 对于数据支持的透明化
    }

    public static DefaultLogPath getDefaultLogPath(){
        return DEFAULT_LOG_PATH;
    }

    /**
     * 暴露info信息 可以通过actuator info endpoint 获取
     * @param key
     * @param value
     */
    public static void exposeInfo(String key,Object value){
        String infoKey = "info." + key;
        System.setProperty(infoKey,String.valueOf(value));
    }

    public static boolean isRunInTest() {
        if (isTest == null) {
            ConfigurableEnvironment environment = (ConfigurableEnvironment) Apps.getEnvironment();
            MutablePropertySources propertySources = environment.getPropertySources();
            isTest = propertySources.contains("Inlined Test Properties");
        }
        return isTest;
    }

    public static String getAppDataPath() {
        if (dataPath != null){
            return dataPath;
        }
        dataPath = System.getProperty("user.home") + "/appdata/" + getAppName() + "/";
        return dataPath;
    }
}
